package com.cloud.bc.controller.ajax.center;

import cn.hutool.core.convert.Convert;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cloud.bc.model.domain.UserInfo;
import com.cloud.bc.model.entity.BcFinances;
import com.cloud.bc.model.response.AjaxResp;
import com.cloud.bc.model.response.RecentChatResponse;
import com.cloud.bc.service.BcFinancesService;
import com.cloud.bc.service.BcUsersService;
import lombok.AllArgsConstructor;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

@AllArgsConstructor
@RestController
@RequestMapping("m/ajax/center/account")
public class Ajax_AccountController {
    private final BcUsersService bcUsersService;
    private final BcFinancesService bcFinancesService;

    /**
     * 兑换
     */
    @PostMapping("exchange")
    public AjaxResp exchange(HttpServletRequest request,
                             HttpServletResponse response,
                             Integer candy
    ) {
        AjaxResp ajaxResp = new AjaxResp();
        if (Convert.toInt(candy, 0) <= 0) {
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("请正确输入兑换金额数量");
            return ajaxResp;
        }
        UserInfo userInfo = bcUsersService.getUserInfo(request);
        BcFinances bcFinances = new BcFinances();
        bcFinances.setUserId(userInfo.getId());
        bcFinances.setCandyPrice(Convert.toBigDecimal(candy));
        boolean b = bcFinancesService.exchange(bcFinances);
        if (!b) {
            ajaxResp.setSuccess(false);
            ajaxResp.setMsg("金额不足,不能正常兑换");
            return ajaxResp;
        }
        return ajaxResp;
    }
}
